import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

import { WindowService } from './window.service';

/**
 * 锁屏状态
 */
export interface LockScreenState {
  locked: boolean;
  password?: string;
}

export const LOCK_SCREEN_STORAGE_KEY = 'LOCK_SCREEN_STORAGE_KEY';

@Injectable({
  providedIn: 'root'
})
export class LockScreenService {
  private lock$ = new BehaviorSubject<LockScreenState>({ locked: false });
  public onLockScreenStateChanged = this.lock$.asObservable();

  constructor(private windowSrc: WindowService) {}

  publish(lss: LockScreenState): void {
    this.windowSrc.setSessionStorage(LOCK_SCREEN_STORAGE_KEY, JSON.stringify(lss));
    this.lock$.next(lss);
  }
}
